package org.example.huadianguanli.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.exception.ErrorException;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.huadianguanli.model.TOrderModel;
import org.example.huadianguanli.model.TOrderShopModel;
import org.example.huadianguanli.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/2/14 23:57
 */
@RestController
@RequestMapping("/order")
public class TOrderController extends BaseController<String, TOrderService, TOrderModel> {

    @Autowired
    private TOrderService tOrderService;

    /**
     * 创建订单
     *
     * @param params
     * @return
     */
    @PostMapping("/create")
    private ApiResult create(@RequestBody Map<String, Object> params) {
        Object shopList = params.get("shopList");
        if (ObjectUtils.isEmpty(shopList)) {
            throw new ErrorException("订单不能为空");
        }
        String userId = getUserId();
        tOrderService.create(params, userId);
        return ApiResult.success();
    }

    /**
     * 订单列表查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/searchPage")
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int limit,
                                @RequestParam(required = false) Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String code = (String) params.get("code");
        SearchPageResult<JSONObject> result = tOrderService.page(page, limit, userId, code);
        return ApiResult.success(result);
    }

    /**
     * 支付
     *
     * @param params
     * @return
     */
    @PostMapping("/pay")
    public ApiResult pay(@RequestBody Map<String, Object> params) {
        Object orderId = params.get("orderId");
        if (ObjectUtils.isEmpty(orderId)) {
            throw new ErrorException("订单不存在！");
        }
        tOrderService.pay(params);
        return ApiResult.success();
    }

    /**
     * 审核
     *
     * @param params
     * @return
     */
    @PostMapping("/examine")
    public ApiResult examine(@RequestBody Map<String, Object> params) {
        Object orderId = params.get("orderId");
        if (ObjectUtils.isEmpty(orderId)) {
            throw new ErrorException("订单不存在！");
        }
        Object status = params.get("status");
        if (ObjectUtils.isEmpty(status)) {
            throw new ErrorException("审核状态不能为空！");
        }
        tOrderService.examine(params);
        return ApiResult.success();
    }
}
